import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Link } from "react-router";
import Util from "../../utils/Util";
import { Tool } from "../../utils/Tool";
import SendModal from "./SendModal";
import * as DataName from '../../action/DataNameConfig';
import { setCommonSystemLoading } from '../../action/Action';
import { GetOrderList } from "../../action/ActionOrder";
import { Table, Button } from "antd";

class TableOrderListMain extends Component {
    constructor(props){
        super(props);
        this.state = {
            ...DataName.ORDER_LIST_FILTER,
            filterStatus: [],
            order_no: ""
        }
        this.handleTableChange=(pagination, filters, sorter)=>{
            console.log(filters)
            let status = 99;
            if(filters.status && filters.status.length > 0){
                status = Number(filters.status[0])
            }
            this.setState({
                page: pagination.current-1,
                size: pagination.pageSize,
                filterStatus: filters.status,
                status
            },()=>{
                this.getData();
            })
        }
        this.getData=(flag)=>{
            let { dispatch } = this.props;
            if(flag){
                dispatch(setCommonSystemLoading(DataName.ORDER_LIST_CARD_LOADING,true));
            }
            dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,true));
            dispatch(GetOrderList({...this.state}));
        }
        this.send=(order_no)=>{
            let { dispatch } = this.props;
            this.setState({
                order_no
            })
            dispatch(setCommonSystemLoading(DataName.BASE_MODAL,true));
        }
    }
    componentWillMount(){
        this.setState({
            uid: this.props.uid || 'system'
        },()=>{
            this.getData(true);
        })
    }
    render() {
        let { BaseData, CommonSystem } = this.props;
        let orderList = BaseData[DataName.ORDER_LIST] || {};
        const pagination = Util.paginationInit(orderList);
        const columns = [
            {
                title: '序号',
                render: (text, record, index) => (
                    <span>{(pagination.current-1)*10 +index+1}</span>
                ),
                key: 'id'
            },
            {
                title: '订单编号',
                width: '200',
                render: (text, record) => (
                    <Link>{/*to={"/app/order/info/"+record.order_no}*/}{record.order_no}</Link>
                ),
                key: 'order_no'
            },
            {
                title: '购买商品',
                width: '300',
                render: (text, record) => (
                    <div>
                        {
                            record.orderProducts.map((res,key)=>{
                                return (
                                    <div key={key}>
                                        <label><strong>{res.product_name}</strong></label>&nbsp;&nbsp;&nbsp;&nbsp;
                                        <span>数量：{res.num}</span>
                                    </div>
                                )
                            })
                        }
                    </div>
                )
            },
            {
                title: '订单价格',
                dataIndex: 'dprice',
                render: (text, record) => (
                    <span>￥{record.dprice}</span>
                )
            },
            {
                title: '订单地址',
                align: 'left',
                render: (text, record) => (
                    <div>
                        <div><strong>收货人地址：</strong>{record.provice_id}{record.city_id}{record.country_id}{record.address}</div>
                        <div><strong>收货人姓名：</strong>{record.consignee}</div>
                        <div><strong>收货人手机号：</strong>{record.phone}</div>
                    </div>
                )
            },
            {
                title: '状态',
                dataIndex: 'status',
                render: (text, record) => (
                    <span>{Util.getOrderStatus(record.status)}</span>
                ),
                filterMultiple: false,
                filters: DataName.ORDER_STATUS_ARRAY,
                filteredValue: this.state.filterStatus || null
            },{
                title: '创建时间',
                render: (text, record) => (
                    <span>{Tool.formatDateTime(record.create_time,1)}</span>
                )
            },
            {
                title: '购买用户',
                render: (text, record) => (
                    <Link>{/*to={"/app/order/info/"+record.uid}*/}{record.orderName}</Link>
                )
            },
            {
                title: '操作',
                render: (text, record) => (
                    <div>
                        {/*<Link to={""}><Button size={"small"}>查看详情</Button></Link><br />*/}
                        {record.status ===2?<Button style={{marginTop:'10px'}} type="primary" size={"small"} onClick={()=>this.send(record.order_no)}>发货</Button>:""}
                    </div>
                )
            }
        ];
        return (
            <div>
                <Table rowKey={"order_no"} columns={columns} dataSource={orderList.content?orderList.content:[]} onChange={this.handleTableChange} bordered={false} loading={CommonSystem[DataName.BASE_TABLE_LOADING] || false} className={"table-center"} pagination={pagination} />
                <SendModal orderNo={this.state.order_no} />
            </div>
        )
    }
}

const TableOrderList = connect(state=>({
    User: state.User,
    CommonSystem: state.CommonSystem,
    BaseData: state.BaseData
}))(TableOrderListMain);

class TableOrderWarningListMain extends Component {
    constructor(props){
        super(props);
        this.handleTableChange=(pagination, filters, sorter)=>{
            let { dispatch } = this.props;
            let pages = pagination.current-1;
            dispatch(setCommonSystemLoading(DataName.BASE_TABLE_LOADING,true));
        }
    }
    componentWillMount(){
    }
    render() {
        let { BaseData, CommonSystem } = this.props;
        let orderList = BaseData[DataName.ORDER_WARNING_LIST] || {};
        const pagination = Util.paginationInit(orderList);
        const columns = [
            {
                title: '主单编号',
                render: (text, record, index) => (
                    <Link onClick={()=>this.getWasteInfo(record.topWasteNo)}>{record.topWasteNo}</Link>
                ),
                key: 'topWasteNo'
            },
            {
                title: '固废发起单位',
                dataIndex: 'fromCompany'
            },
            {
                title: '固废类型',
                dataIndex: 'wasteType',
                render: (text, record) => (
                    <span>{Util.getOrderType(record.wasteType)}</span>
                )
            },
            {
                title: '逾期时间',
                dataIndex: 'isDeal',
                render: (text, record) => (
                    <span>{record.isDeal}天</span>
                )
            },
            {
                title: '处理逾期单',
                dataIndex: 'num4'
            }
        ];
        return (
            <div>
                <Table rowkey={"topWasteNo"} columns={columns} dataSource={orderList.content?orderList.content:[]} onChange={this.handleTableChange} bordered={false} loading={CommonSystem[DataName.BASE_TABLE_LOADING] || false} className={"table-center"} pagination={pagination} />
            </div>
        )
    }
}

const TableOrderWarningList = connect(state=>({
    User: state.User,
    CommonSystem: state.CommonSystem,
    BaseData: state.BaseData
}))(TableOrderWarningListMain);

export {
    TableOrderList,
    TableOrderWarningList
}